package io.renren.modules.generator.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.utils.excel.ExcelUtils;
import io.renren.modules.generator.entity.ProjectEntity;
import io.renren.modules.generator.export.ProjectExcelDataCreaterImpl;
import io.renren.modules.generator.service.ProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-11-26 15:10:04
 */
@RestController
@RequestMapping("generator/project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @Autowired
    private ExcelUtils excelUtils;

    @Autowired
    private ProjectExcelDataCreaterImpl projectExcelDataCreater;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){

        Query query = new Query(params);

        List<ProjectEntity> list = projectService.queryPage(query);

        int total =projectService.queryPageTotal(params);

        PageUtils pageUtils = new PageUtils(list, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtils);

    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
			ProjectEntity project = projectService.selectById(id);

        return R.ok().put("project", project);
    }

    /**
     * 保存
     */
    @CacheEvict(value = {"projectController:queryEntity"},allEntries = true)
    @RequestMapping("/save")
    @RequiresPermissions("generator:project:save")
    public R save(@RequestBody ProjectEntity project){
			projectService.insert(project);

        return R.ok();
    }

    /**
     * 修改
     */
    @CacheEvict(value = {"projectController:queryEntity"},allEntries = true)
    @RequestMapping("/update")
    @RequiresPermissions("generator:project:update")
    public R update(@RequestBody ProjectEntity project){
			projectService.updateById(project);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:project:delete")
    public R delete(@RequestBody Integer[] ids){
			projectService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 更新状态
     * @param ids
     * @return
     */
    @CacheEvict(value = {"projectController:queryEntity"},allEntries = true)
    @RequestMapping("/updateStatusByIds")
    @RequiresPermissions("generator:project:status")
    public R updateStatusByIds(@RequestBody Integer[] ids){
        projectService.updateStatusByIds(Arrays.asList(ids));
        return R.ok();
    }

    @Cacheable("projectController:queryEntity")
    @PostMapping("/queryEntity")
    public R queryEntity(@RequestBody Map<String,Object> params){
        List<ProjectEntity> entities = projectService.selectByMap(params);
        return R.ok().put("data",entities);
    }



    @GetMapping("export")
    @RequiresPermissions("generator:project:export")
    public void exportDate(@RequestParam Map<String, Object> params, HttpServletRequest request,
                           HttpServletResponse response) throws Exception {
        String[] lableNames = {"项目代码","项目名称","项目属性","激活时间","备注","预留字段1","预留字段2","预留字段3"};
        String[] columnNames={"code","name","propertys","activatedTime","remark","extendFirst","extendSecond","extendThird"};
        excelUtils.export(response,"数据导出.xlsx",lableNames,columnNames,params,projectExcelDataCreater);

    }


}
